using System;
using System.Collections;
using System.Collections.Generic;
using JetBrains.Annotations;
using UnityEngine;
using UnityEngine.Events;
using UnityEngine.InputSystem;


public class PlayerInputController : MonoBehaviour
{
    public PlayerKeyInput playerInput;
    public PhysicsCheck physicsCheck;
    public PlayerAnimationController anim;

    public Playercharacter playercharacter;
   // public ProjectWinter WinterProject;
    
    public Vector2 moveInput;
    public Rigidbody2D rb;
    [Header("基本参数")]
    public float moveSpeed;
    public float runSpeed;
    public float hurtForce;
    
    public Boolean isLAttack;
    public Boolean isMove;
    public Boolean isRun;
    public Boolean isHurt;
    public Boolean isDead;
    [Header("输入控制关启")]
    public bool inputDisable;
    public UnityEvent<bool> onRun;
    

    private void Awake() { 
        playerInput = new PlayerKeyInput();
        physicsCheck = GetComponent<PhysicsCheck>();
        playercharacter = GetComponent<Playercharacter>();
        anim = GetComponent<PlayerAnimationController>();
        playerInput.GamePlay.Run.started += TriggerTRun;
        playerInput.GamePlay.Run.canceled += TriggerFRun;
        playerInput.GamePlay.UseItem1.started += OnUseItem1;
        playerInput.GamePlay.UseItem2.started += OnUseItem2;
        playerInput.GamePlay.UseItem3.started += OnUseItem3;
        playerInput.GamePlay.UseItem4.started += OnUseItem4;
        
    }
    


    private void OnEnable() { 
        playerInput.Enable();
        EventHandler.MoveToPosition += OnMoveToPosition;
        EventHandler.UpdateGameStateEvent += OnUpdateGameStateEvent;
    }
        
        
    private void OnDisable(){
        playerInput.Disable();
        EventHandler.MoveToPosition -= OnMoveToPosition;
        EventHandler.UpdateGameStateEvent -= OnUpdateGameStateEvent;
    }

    private void Update() { 
        if (!inputDisable)
            moveInput = playerInput.GamePlay.Move.ReadValue<Vector2>();
        else
            isRun = false;
        IsMove();
    }
    private void FixedUpdate() {
        if (!isRun)
        {
            Move();
        }
        else
        {
            onRun?.Invoke(isRun);
            Run();
        }

    }

    private void Move() {
      
           rb.velocity = new Vector2(moveInput.x * moveSpeed * Time.deltaTime,
                moveInput.y * moveSpeed * Time.deltaTime);
            /*int faceDir = (int)transform.localScale.x;
            if (moveInput.x > 0)
            {
                faceDir = -1;
            }
            else if (moveInput.x < 0)
            {
                faceDir = 1;
            }
            transform.localScale = new Vector3(faceDir, 1, 1);*/
        
    }

    private void Run()
    {
        rb.velocity = new Vector2(moveInput.x * runSpeed * Time.deltaTime,
                moveInput.y * runSpeed * Time.deltaTime);
            int faceDir = (int)transform.localScale.x;
            if (moveInput.x > 0)
            {
                faceDir = -1;
            }
            else if (moveInput.x < 0)
            {
                faceDir = 1;
            }

            transform.localScale = new Vector3(faceDir, 1, 1);
        
    }

    private void TriggerTRun(InputAction.CallbackContext ctx)
    {
        isRun = true;
    }
    private void TriggerFRun(InputAction.CallbackContext obj)
    {
        isRun = false;
    }

    public void ByHit(Transform attacker)
    {
        isHurt = true;
        rb.velocity = Vector2.zero;
        Vector2 dir = new Vector2( (transform.position.x-attacker.position.x) , 0).normalized;
        rb.AddForce(dir*hurtForce, ForceMode2D.Impulse);
    }
    
    public void PlayerDead()
    {
        isDead = true;
        playerInput.GamePlay.Disable();
    }
    private void OnMoveToPosition(Vector3 targetPosition)
    {
        transform.position = targetPosition;
    }
    private void OnUpdateGameStateEvent(GameState gameState)
    {
        switch (gameState)
        {
            case GameState.Gameplay:
                inputDisable = false;
                break;
            case GameState.Pause:
                inputDisable = true;
                moveInput = new Vector2(0, 0);
                break;
        }
    }

    private void IsMove()
    {
        if (moveInput != Vector2.zero)
        {
            isMove = true;
        }
        else
        {
            isMove = false;
        }
    }

    public Vector3 GetMousePosition()
    {
        Vector2 mouseScreenPosition = Mouse.current.position.ReadValue();
       
        Vector3 mouseWorldPosition = Camera.main.ScreenToWorldPoint(new Vector3(mouseScreenPosition.x, mouseScreenPosition.y, 0));
        
        mouseWorldPosition.z = 0;
        return mouseWorldPosition;
    }

 

    private void OnUseItem1(InputAction.CallbackContext obj)
    {
        playercharacter.UseItem(1);
    }
    
    private void OnUseItem2(InputAction.CallbackContext obj)
    {
        playercharacter.UseItem(2);
    }
    
    private void OnUseItem3(InputAction.CallbackContext obj)
    {
        playercharacter.UseItem(3);
    }

    private void OnUseItem4(InputAction.CallbackContext obj)
    {
        playercharacter.UseItem(4);
    }
    
}
